package com.redoor.app.stake.auth.controller;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.model.controllerModel.BaseRoleActionBean;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.redoor.app.stake.auth.model.BaseRole;
import com.redoor.app.stake.auth.model.BaseRoleCriteria;
import com.redoor.app.stake.auth.service.BaseRoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* Created by wangjunlei on 2018-02-07 15:19:13.
*/
@RestController
@RequestMapping("/base/roles")
public class BaseRoleController {

    @Autowired
    private BaseRoleService baseRoleService;
    @Autowired
    private BaseUserService baseUserService;

    @PutMapping
    public ResponseResult update(@RequestBody BaseRole baseRole) {
        baseRoleService.updateBySelective(baseRole);
        return ResultGenerator.generatorSuccess();
    }
    @ApiOperation(value = "根据主键查询角色对象传入为id='主键'", notes = "根据主键查询角色对象")
    @PostMapping("/detail")
    public ResponseResult detail( String id) {
        BaseRole baseRole = baseRoleService.findById(id);
        return ResultGenerator.generatorSuccess(baseRole);
    }

    @ApiOperation(value = "无条件询角色主列表",notes = "无条件询角色主列表")
    @GetMapping("/listall")
    public ResponseResult list() {
        List<BaseRole> list = baseRoleService.findRoleAll();
        return ResultGenerator.generatorSuccess(list);
    }

    /**
     * 查询主列表
     * @Author zhangYiHang
     * @Date 2018/3/8 20:51
     */
    @ApiOperation(value = "根据条件查询角色主列表",notes = "根据条件查询角色主列表")
    @GetMapping("/list")
    public ResponseResult qryRole(BaseRoleCriteria baseRoleCriteria){
        List<BaseRole> list = this.baseRoleService.qryRole(baseRoleCriteria);
        return ResultGenerator.generatorSuccess(list);
    }
    /**
     * 新增角色
     * @Author zhangYiHang
     * @Date 2018/3/9 9:38
     */
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @RequiresPermissions("sys:role:insert")
    @PostMapping("/insert")
    public ResponseResult insertRole(BaseRoleCriteria baseRoleCriteria){
        try {
            this.baseRoleService.insertRole(baseRoleCriteria);
            return ResultGenerator.generatorSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据主键删除角色
     * @Author zhangYiHang
     * @Date 2018/3/9 9:56
     */
    @ApiOperation(value = "根据主键删除角色传入uuid='主键'", notes = "根据主键删除角色")
    @RequiresPermissions("sys:role:delete")
    @PostMapping("/del")
    public ResponseResult deleteRole(String uuid){
        try {
            BaseUser loginUser = this.baseUserService.getShiroUser(null);
            BaseRole baseRole = new BaseRole();
            baseRole.setUpdatedat(new Date());
            baseRole.setUpdateruuid(loginUser.getUuid());
            baseRole.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
            baseRole.setUuid(uuid);
            this.baseRoleService.updateByStatus(baseRole);
            return ResultGenerator.generatorSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据主键修改角色
     * @Author zhangYiHang
     * @Date 2018/3/9 10:01
     */
    @ApiOperation(value = "根据主键修改角色", notes = "根据主键修改角色")
    @RequiresPermissions("sys:role:update")
    @PostMapping("/update")
    public ResponseResult updateRole(BaseRoleCriteria baseRoleCriteria){
        try {
            this.baseRoleService.updateByUUid(baseRoleCriteria);
            return ResultGenerator.generatorSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据用户的uuid查询用户角色
     * @Author zhangYiHang
     * @Date 2018/3/7 17:58
     */
    @ApiOperation(value = "传入数据为用户UUID查询所拥有的角色", notes = "传入数据为用户UUID查询所拥有的角色")
    @RequiresPermissions("sys:role:list")
    @PostMapping("/qryByUserUuid")
    public ResponseResult qryUserRoleByUuid(String uuid){
        List<BaseRole> list = this.baseRoleService.qryUserRoleByUuid(uuid);
        return ResultGenerator.generatorSuccess(list);
    }
    /**
     * 根据角色名称查询数据数量
     * @Author zhangYiHang
     * @Date 2018/3/26 11:51
     * @Param
     */
    @ApiOperation(value = "根据角色名称查询数据数量")
    @ApiImplicitParam(name = "roleName", value = "角色名称", paramType = "query", dataType = "String", required = true, defaultValue = "")
    @PostMapping("/qryRoleCountByRoleName")
    public ResponseResult qryRoleCountByRoleName(String roleName){
        Integer count = this.baseRoleService.qryRoleCountByRoleName(roleName);
        return ResultGenerator.generatorSuccess(count);
    }
}
